//
//  PersonCenterViewController.m
//  联付商圈
//
//  Created by apple on 2017/10/30.
//  Copyright © 2017年 lianfu. All rights reserved.
//

#import "PersonCenterViewController.h"
#import "PersonCenterCollectionViewCell.h"
#import "TaskCollectionViewCell.h"
#import "PersonCenterView.h"

@interface PersonCenterViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UIScrollView *personCenterScrollView;
///头像
@property (nonatomic, strong) UIImageView *headerImgView;
///用户ID
@property (nonatomic, strong) UILabel *userIDLabel;
///商户名称
@property (nonatomic, strong) UILabel *merchantLabel;
///任务
@property (nonatomic, strong) UIImageView *taskView;
///任务数组
@property (nonatomic, strong) NSArray *taskArr;
///我的钱包
@property (nonatomic, strong) PersonCenterView *walletView;
///我的钱包数组
@property (nonatomic, strong) NSArray *walletArr;
///我的相关
@property (nonatomic, strong) PersonCenterView *aboutMeView;
///我的相关数组
@property (nonatomic, strong) NSArray *aboutMeArr;
///设置
@property (nonatomic, strong) PersonCenterView *settingView;
///设置数组
@property (nonatomic, strong) NSArray *settingArr;
@end

@implementation PersonCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self buildMainInterface];
    //任务(发布,跑腿,接受)
    [self addTaskView];
    //我的钱包
    [self addMyWallet];
    //我的相关
    [self addAboutMe];
    //设置
    [self addSetting];
}

- (void)loadData
{
    self.taskArr = @[@[@"我发布的任务", @"跑腿任务", @"我接受的任务"], @[@"publish@2x", @"runErrands@2x", @"task@2x"]];
    self.walletArr = @[@[@"我的收入", @"提现", @"充值", @"银行卡", @"余额"], @[@"income@2x", @"withdraw@2x", @"recharge@2x", @"banckCard@2x", @"money@2x"]];
    self.aboutMeArr = @[@[@"商户入驻", @"实名认证", @"收银台"], @[@"businessSettled@2x", @"trueName@2x", @"checkout@2x"]];
    self.settingArr = @[@[@"个人信息设置", @"安全中心", @"清除缓存", @"关于联付商圈", @"去评分", @"版本更新"], @[@"my@2x", @"safe@2x", @"clearCache@2x", @"about@2x", @"star@2x", @"update@2x"]];
}

- (void)buildMainInterface
{
    self.personCenterScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH - 49)];
    self.personCenterScrollView.contentSize = CGSizeMake(SCREENW, 1176 * FONTSIZE);
    [self.view addSubview:_personCenterScrollView];
    
    UIImageView *topBackImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREENW, 243 * FONTSIZE)];
    topBackImgView.image = [UIImage imageNamed:@"headerBack@2x"];
    [self.personCenterScrollView addSubview:topBackImgView];
    
    self.headerImgView = [[UIImageView alloc] initWithFrame:CGRectMake(151 * FONTSIZE, 55 * FONTSIZE, 74 * FONTSIZE, 74 * FONTSIZE)];
    self.headerImgView.image = [UIImage imageNamed:@"header@2x"];
    [self.personCenterScrollView addSubview:_headerImgView];
    
    UITapGestureRecognizer *changeInfoTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(turnToChangeInfoViewController)];
    self.headerImgView.userInteractionEnabled = YES;
    [self.headerImgView addGestureRecognizer:changeInfoTap];
    
    self.userIDLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 141 * FONTSIZE, SCREENW, 17 * FONTSIZE)];
    self.userIDLabel.textColor = [UIColor whiteColor];
    self.userIDLabel.textAlignment = 1;
    self.userIDLabel.font = [UIFont systemFontOfSize:17 * FONTSIZE];
    self.userIDLabel.text = @"用户ID:1234567890";
    [self.personCenterScrollView addSubview:_userIDLabel];
    
    self.merchantLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 168.5 * FONTSIZE, SCREENW, 17 * FONTSIZE)];
    self.merchantLabel.textColor = [UIColor whiteColor];
    self.merchantLabel.textAlignment = 1;
    self.merchantLabel.font = [UIFont systemFontOfSize:17 * FONTSIZE];
    self.merchantLabel.text = @"商户名称:联付科技";
    [self.personCenterScrollView addSubview:_merchantLabel];
}

- (void)turnToChangeInfoViewController
{
    PersonInfoViewController *infoVC = [[PersonInfoViewController alloc] init];
    [self.navigationController pushViewController:infoVC animated:YES];
}

- (void)addCollectionViewOnWhichView:(UIView *)view ByTag:(NSInteger)tag WhichCell:(NSInteger)whichCell CellIdentifier:(NSString *)identifier ItemCount:(NSInteger)itemCount Frame:(CGRect)frame
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.itemSize = CGSizeMake((SCREENW - 36 *FONTSIZE) / 3, frame.size.height);
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, flowLayout.itemSize.width * 3, frame.size.height * itemCount) collectionViewLayout:flowLayout];
    collectionView.tag = tag;
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.scrollEnabled = NO;
    [view addSubview:collectionView];
    
    if (whichCell == 0) {
        [collectionView registerClass:[PersonCenterCollectionViewCell class] forCellWithReuseIdentifier:identifier];
    } else {
        [collectionView registerClass:[TaskCollectionViewCell class] forCellWithReuseIdentifier:identifier];
    }
}

- (void)addTaskView
{
    self.taskView = [[UIImageView alloc] initWithFrame:CGRectMake(13 * FONTSIZE, 207 * FONTSIZE, 352 * FONTSIZE, 172 * FONTSIZE)];
    self.taskView.image = [UIImage imageNamed:@"roundedRectangle@2x"];
    self.taskView.userInteractionEnabled = YES;
    [self.personCenterScrollView addSubview:_taskView];
    
    UILabel *allTaskLabel = [[UILabel alloc] init];
    allTaskLabel.text = @"查看全部任务";
    allTaskLabel.textAlignment = 1;
    allTaskLabel.font = [UIFont systemFontOfSize:15 * FONTSIZE];
    [self.taskView addSubview:allTaskLabel];
    [allTaskLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.taskView.mas_left).offset(120 * FONTSIZE);
        make.right.equalTo(self.taskView.mas_right).offset(-138 * FONTSIZE);
        make.bottom.equalTo(self.taskView.mas_bottom).offset(-27 * FONTSIZE);
        make.height.mas_equalTo(15 * FONTSIZE);
    }];
    
    UIImageView *allTaskImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"personBack@3x"]];
    [self.taskView addSubview:allTaskImgView];
    [allTaskImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(allTaskLabel.mas_right).offset(5 * FONTSIZE);
        make.bottom.equalTo(self.taskView.mas_bottom).offset(-27 * FONTSIZE);
        make.width.mas_equalTo(15 * FONTSIZE);
        make.height.mas_equalTo(15 * FONTSIZE);
    }];
    
    [self addCollectionViewOnWhichView:self.taskView ByTag:0 WhichCell:1 CellIdentifier:@"taskCell" ItemCount:1 Frame:CGRectMake(8 * FONTSIZE, 20 * FONTSIZE, self.taskView.frame.size.width - 16 * FONTSIZE, 86 * FONTSIZE)];
}

- (void)addMyWallet
{
    self.walletView = [[PersonCenterView alloc] initWithFrame:CGRectMake(18 * FONTSIZE, self.taskView.frame.origin.y + self.taskView.frame.size.height, SCREENW - 36 * FONTSIZE, 242 * FONTSIZE + 10 * FONTSIZE)];
    self.walletView.titleLabel.text = @"我的钱包";
    [self.personCenterScrollView addSubview:_walletView];
    
    [self addCollectionViewOnWhichView:self.walletView ByTag:1 WhichCell:0 CellIdentifier:@"walletCell" ItemCount:2 Frame:CGRectMake(0, 70 * FONTSIZE, 0, 86 * FONTSIZE)];
}

- (void)addAboutMe
{
    self.aboutMeView = [[PersonCenterView alloc] initWithFrame:CGRectMake(self.walletView.frame.origin.x, self.walletView.frame.origin.y + self.walletView.frame.size.height, self.walletView.frame.size.width, 156 * FONTSIZE + 10 * FONTSIZE)];
    self.aboutMeView.titleLabel.text = @"我的相关";
    [self.personCenterScrollView addSubview:_aboutMeView];
    
    [self addCollectionViewOnWhichView:self.aboutMeView ByTag:2 WhichCell:0 CellIdentifier:@"aboutMeCell" ItemCount:1 Frame:CGRectMake(0, 70 * FONTSIZE, 0, 86 * FONTSIZE)];
}

- (void)addSetting
{
    self.settingView = [[PersonCenterView alloc] initWithFrame:CGRectMake(18 * FONTSIZE, self.aboutMeView.frame.origin.y + self.aboutMeView.frame.size.height, SCREENW - 36 * FONTSIZE, 242 * FONTSIZE + 10 * FONTSIZE)];
    self.settingView.titleLabel.text = @"设置";
    [self.personCenterScrollView addSubview:_settingView];
    
    [self addCollectionViewOnWhichView:self.settingView ByTag:3 WhichCell:0 CellIdentifier:@"settingCell" ItemCount:2 Frame:CGRectMake(0, 70 * FONTSIZE, 0, 86 * FONTSIZE)];
    
    UIButton *exitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    exitButton.frame = CGRectMake(48 * FONTSIZE, self.settingView.frame.origin.y + self.settingView.frame.size.height + 41 * FONTSIZE, SCREENW - 96 * FONTSIZE, 40 * FONTSIZE);
    exitButton.layer.cornerRadius = 5;
    exitButton.layer.backgroundColor = MAINCOLOR.CGColor;
    [exitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [exitButton setTitle:@"退出登录" forState:UIControlStateNormal];
    [exitButton.titleLabel setFont:[UIFont systemFontOfSize:17 * FONTSIZE]];
    [self.personCenterScrollView addSubview:exitButton];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    switch (collectionView.tag) {
        case 0:{
            return [[self.taskArr firstObject] count];
        }
            break;
        case 1:{
            return [[self.walletArr firstObject] count];
        }
            break;
        case 2:{
            return [[self.aboutMeArr firstObject] count];
        }
            break;
        case 3:{
            return [[self.settingArr firstObject] count];
        }
            break;
        default:
            break;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (collectionView.tag) {
        case 0:{
            TaskCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"taskCell" forIndexPath:indexPath];
            cell.label.text = [[self.taskArr firstObject] objectAtIndex:indexPath.row];
            cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", [self.taskArr.lastObject objectAtIndex:indexPath.row]]];
            return cell;
        }
            break;
        case 1:{
            PersonCenterCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"walletCell" forIndexPath:indexPath];
            cell.label.text = [[self.walletArr firstObject] objectAtIndex:indexPath.row];
            cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", [self.walletArr.lastObject objectAtIndex:indexPath.row]]];
            return cell;
        }
            break;
        case 2:{
            PersonCenterCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"aboutMeCell" forIndexPath:indexPath];
            cell.label.text = [[self.aboutMeArr firstObject] objectAtIndex:indexPath.row];
            cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", [self.aboutMeArr.lastObject objectAtIndex:indexPath.row]]];
            return cell;
        }
            break;
        case 3:{
            PersonCenterCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"settingCell" forIndexPath:indexPath];
            cell.label.text = [[self.settingArr firstObject] objectAtIndex:indexPath.row];
            cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", [self.settingArr.lastObject objectAtIndex:indexPath.row]]];
            return cell;
        }
            break;
        default:
            break;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (collectionView.tag) {
        case 0:{
            NSLog(@"%@", [[self.taskArr firstObject] objectAtIndex:indexPath.row]);
        }
            break;
        case 1:{
            NSLog(@"%@", [[self.walletArr firstObject] objectAtIndex:indexPath.row]);
        }
            break;
        case 2:{
            if (indexPath.row == 0) {
                
            } else if (indexPath.row == 1) {
                TrueNameViewController *trueNameVC = [[TrueNameViewController alloc] init];
                [self.navigationController pushViewController:trueNameVC animated:YES];
            } else if (indexPath.row == 2) {

            }
            NSLog(@"%@", [[self.aboutMeArr firstObject] objectAtIndex:indexPath.row]);
        }
            break;
        case 3:{
            if (indexPath.row == 0) {
                
            } else if (indexPath.row == 1) {
                SafeCenterViewController *safeCenterVC = [[SafeCenterViewController alloc] init];
                [self.navigationController pushViewController:safeCenterVC animated:YES];
            } else if (indexPath.row == 2) {
                
            } else if (indexPath.row == 3) {
                
            } else if (indexPath.row == 4) {
                
            } else {
                
            }
        }
            break;
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
